package com.linxz.video.videoplayer.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

import com.linxz.video.R;
import com.linxz.video.videoplayer.event.FullToSmallListener;
import com.linxz.video.videoplayer.event.VideoPlayerListener;
import com.linxz.video.videoplayer.utils.Utils;

/**
 * @author Linxz
 * 创建日期：2019年10月15日 18:16
 * version：v4.5.4
 * 描述：
 */
public class VideoFullDialog extends Dialog {

    private CustomVideoView mVideoView;
    private RelativeLayout mRootView;
    private ViewGroup mParentView;

    private int mPosition;
    private FullToSmallListener mListener;
    private boolean isFirst = true;

    private Bundle mStartBundle;
    /**
     * 用于Dialog出入场动画
     * */
    private Bundle mEndBundle;

    /**
     * 动画要执行的平移值
     * */
    private int deltaY;

    public VideoFullDialog(Context context,CustomVideoView videoView,int position ){
        super(context, R.style.dialog_full_screen);
        this.mVideoView=videoView;
        this.mPosition=position;
    }

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window=getWindow();
        if (window!=null){
           window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.lib_video_dialog_video_layout);
        initVideoView();
    }

    private void initVideoView() {
        mParentView = (RelativeLayout) findViewById(R.id.content_layout);
        mRootView = findViewById(R.id.root_view);
        mRootView.setVisibility(View.INVISIBLE);

        mVideoView.setVideoPlayerListener(mVideoPlayerListener);
        mVideoView.mute(false);
        mParentView.addView(mVideoView);
        mParentView.getViewTreeObserver()
                .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        mParentView.getViewTreeObserver().removeOnPreDrawListener(this);
                        prepareScene();
                        runEnterAnimation();
                        return true;
                    }
                });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        //防止第一次，有些手机仍显示全屏按钮
        mVideoView.isShowFullBtn(false);
        if (!hasFocus) {
            mPosition = mVideoView.getCurrentPosition();
            mVideoView.pauseForFullScreen();
        } else {
            if (isFirst) { //为了适配某些手机不执行seekandresume中的播放方法
                mVideoView.seekAndResume(mPosition);
            } else {
                mVideoView.resume();
            }
        }
        isFirst = false;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        runExitAnimator();
    }

    public void setViewBundle(Bundle bundle) {
        mStartBundle = bundle;
    }

    public void setFullToSmallListener(FullToSmallListener listener){
        this.mListener=listener;
    }

    //准备动画所需数据
    private void prepareScene() {
        mEndBundle = Utils.getViewProperty(mVideoView);
        /**
         * 将desationview移到originalview位置处
         */
        deltaY = (mStartBundle.getInt(Utils.PROPNAME_SCREENLOCATION_TOP) - mEndBundle.getInt(
                Utils.PROPNAME_SCREENLOCATION_TOP));
        mVideoView.setTranslationY(deltaY);
    }

    //准备入场动画
    private void runEnterAnimation() {
        mVideoView.animate()
                .setDuration(200)
                .setInterpolator(new LinearInterpolator())
                .translationY(0)
                .withStartAction(new Runnable() {
                    @Override
                    public void run() {
                        mRootView.setVisibility(View.VISIBLE);
                    }
                })
                .start();
    }

    //准备出场动画
    private void runExitAnimator() {
        mVideoView.animate()
                .setDuration(200)
                .setInterpolator(new LinearInterpolator())
                .translationY(deltaY)
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        dismiss();
                        if (mListener != null) {
                            mListener.getCurrentPlayPosition(mVideoView.getCurrentPosition());
                        }
                    }
                })
                .start();
    }

    @Override
    public void dismiss() {
        mParentView.removeView(mVideoView);
        super.dismiss();
    }



    private VideoPlayerListener mVideoPlayerListener=new VideoPlayerListener(){
        @Override
        public void onPlayClick() {
            mVideoView.resume();
        }

        @Override
        public void onVideoClick() {
            if (mVideoView.isPlaying()){
                mVideoView.pause();
            }else{
                mVideoView.resume();
            }
        }

        @Override
        public void onVideoLoadSuccess() {
            if (mVideoView != null) {
                mVideoView.resume();
            }
        }

        @Override
        public void onVideoLoadError() {

        }

        @Override
        public void onVideoLoadComplete() {
            dismiss();
            if (mListener != null) {
                mListener.playComplete();
            }
        }

        @Override
        public void onFullScreenClick() {

        }

        @Override
        public void onSmallScreenClick() {
            runExitAnimator();
        }
    };


}
